/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.signInRecord.service;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.signInRecord.domain.SignInRecord;
import com.fzhucc.modules.signInRecord.domain.dto.SignInRecordAddDto;
import com.fzhucc.utils.*;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.signInRecord.repository.SignInRecordRepository;
import com.fzhucc.modules.signInRecord.domain.dto.SignInRecordDto;
import com.fzhucc.modules.signInRecord.domain.dto.SignInRecordQueryCriteria;
import com.fzhucc.modules.signInRecord.service.mapstruct.SignInRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.sql.Timestamp;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2022-12-09
**/
@Service
@RequiredArgsConstructor
public class SignInRecordService {

    private final SignInRecordRepository signInRecordRepository;
    private final SignInRecordMapper signInRecordMapper;

    public Map<String,Object> queryAll(SignInRecordQueryCriteria criteria, Pageable pageable){
        Page<SignInRecord> page = signInRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(signInRecordMapper::toDto));
    }

    public List<SignInRecordDto> queryAll(SignInRecordQueryCriteria criteria){
        return signInRecordMapper.toDto(signInRecordRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    public SignInRecordDto findById(Integer id) {
        SignInRecord signInRecord = signInRecordRepository.findById(id).orElseGet(SignInRecord::new);
        ValidationUtil.isNull(signInRecord.getId(),"SignInRecord","id",id);
        return signInRecordMapper.toDto(signInRecord);
    }

    @Transactional(rollbackFor = Exception.class)
    public SignInRecordDto create(SignInRecordAddDto signInRecordAddDto) {
        SignInRecord resources = new SignInRecord();
        resources.setName(signInRecordAddDto.getName());
        resources.setCompany(signInRecordAddDto.getCompany());
        resources.setPhone(signInRecordAddDto.getPhone());
        resources.setActivityId(signInRecordAddDto.getActivityId());
        resources.setSiginInTime(new Timestamp(new Date().getTime()));
        return signInRecordMapper.toDto(signInRecordRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(SignInRecord resources) {
        SignInRecord signInRecord = signInRecordRepository.findById(resources.getId()).orElseGet(SignInRecord::new);
        ValidationUtil.isNull( signInRecord.getId(),"SignInRecord","id",resources.getId());
        signInRecord.copy(resources);
        signInRecordRepository.save(signInRecord);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            signInRecordRepository.deleteById(id);
        }
    }

    public void download(List<SignInRecordDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SignInRecordDto signInRecord : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("签到人", signInRecord.getName());
            map.put("联系方式", signInRecord.getPhone());
            map.put("公司/团队", signInRecord.getCompany());
            map.put("签到时间", signInRecord.getSiginInTime());
            map.put("活动Id", signInRecord.getActivityId());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}